package com.ocom.payconfig.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.payconfig.EntityReviewOrder;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.enums.ReviewOrderEnum;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoReviewOrder;
import com.ocom.payconfig.service.ReviewOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ReviewOrderServiceImpl extends ServiceImpl<IDaoReviewOrder, EntityReviewOrder> implements ReviewOrderService {

    @Autowired
    private IDaoReviewOrder iDaoReviewOrder;

//    @Value("${deshengapi}")
//    private  String dsUrl;

    private final static String TECSUN_KEY = "6wIKLiPeKz63g3MCZBX0eQc1OAPbodJ6k5cV";//德生
//    private final static String TECSUN_URL = "https://xyykt-pc.d-cos.com/prod-api/open-api/xak/reviewProcess";//德生
    private  String TECSUN_URL = "/open-api/xak/reviewProcess";//德生

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId")
    public boolean addReviewOrder(Long comId, EntityReviewOrder entityReviewOrder) {
        return this.save(entityReviewOrder);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId")
    public boolean updateReviewOrder(Long comId, EntityReviewOrder entityReviewOrder) {
        UpdateWrapper<EntityReviewOrder> updateWrapper = Wrappers.update();
        updateWrapper.eq("com_id", comId);
        updateWrapper.eq("order_no", entityReviewOrder.getOrderNo());
        updateWrapper.set("status", entityReviewOrder.getStatus());
        updateWrapper.set("update_time", new Date());
        updateWrapper.set("remark", entityReviewOrder.getRemark());
        return this.update(updateWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId", isMaster = MasterSlaveEnum.SLAVE)
    public EntityReviewOrder getReviewOrder(Long comId, EntityReviewOrder entityReviewOrder) {
        QueryWrapper<EntityReviewOrder> queryWrapper = Wrappers.query();
        queryWrapper.eq("com_id", comId);
        queryWrapper.eq("order_no", entityReviewOrder.getOrderNo());
        queryWrapper.last("limit 1");
        return this.getOne(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId", isMaster = MasterSlaveEnum.SLAVE)
    public List<EntityReviewOrder> findByStatus(Long comId, Integer status) {
        QueryWrapper<EntityReviewOrder> queryWrapper = Wrappers.query();
        queryWrapper.eq("status", status);
        return this.list(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId", isMaster = MasterSlaveEnum.SLAVE)
    public void reviewOrderTask(Long comId, Integer status) {
        List<EntityReviewOrder> reviewOrders = findByStatus(comId, status);

        for (EntityReviewOrder entityReviewOrder : reviewOrders) {

            switch (entityReviewOrder.getThemeNo()) {
                case ReviewOrderEnum.SUBSIDY_SEND_V:
                case ReviewOrderEnum.COUPON_SEND_V:
                case ReviewOrderEnum.COUPON_MANUAL_V:
                case ReviewOrderEnum.WALLET_EXAMINE_V:
                    subsidySend(entityReviewOrder);
                    break;
            }
        }
    }

    private void subsidySend(EntityReviewOrder entityReviewOrder) {
//        try {
//
//            ReviewProcessRequest request = new ReviewProcessRequest();
//            request.setOrgNum(entityReviewOrder.getComId().toString());
//            request.setNonceStr(UUIDUtils.getGUID32());
//            request.setTimestamp(new Date().getTime());
//
//            request.setOrderNo(entityReviewOrder.getOrderNo());
//            request.setThemeNo(entityReviewOrder.getThemeNo());
//
//            ReviewProcessRequest.Theme theme = request.getThemeObject();
//            theme.setPageUrl(entityReviewOrder.getPageUrl());
//            theme.setContent(entityReviewOrder.getRemark());
//            if (null != entityReviewOrder.getEndTime()) {
//                try {
//                    theme.setEndTime(DateUtils.dateToStr(entityReviewOrder.getEndTime()));
//                } catch (Exception e) {
//
//                    e.printStackTrace();
//                }
//            }
//            request.setThemeObject(theme);
//            String sign = SignUtils.createSign(request, null, TECSUN_KEY);
//            request.setSign(sign);
//
//            JSONObject requestBody = JSON.parseObject(JSON.toJSONString(request));
//            log.info("=================================开始推送审核工单：参数：url"+dsUrl+TECSUN_URL+"&&" + requestBody);
//            String json = HttpUtils.postsJson(dsUrl+TECSUN_URL, requestBody);
//            log.info("====================json：" + json);
//            Result results = JSON.parseObject(json, Result.class);
//            log.info("====================results：" + results.toString());
//            if (200 != results.getCode()) {
//                log.info(entityReviewOrder.getOrderNo() + "提交工单审核失败！");
////                entityReviewOrder.setStatus(4);
//                entityReviewOrder.setRemark("提交工单审核失败!响应结果：" + results.toString());
//            } else {
//                entityReviewOrder.setStatus(2);
//            }
//                updateReviewOrder(entityReviewOrder.getComId(), entityReviewOrder);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

    }

    public static void main(String[] args) {
        System.out.println(new Date().getTime());
    }
}
